home *** CD-ROM | disk | FTP | other *** search
- Expression Evaluator and TAxesView ReadMe
- Daniel Bryant (c)1997
- --------------------------------------------------------------------
-
- Installing and running the software:
- Simply move the uninstalled files to the directory of your
- choice and run the executable (mathtstp.exe) No files are needed
- in the run-path in order for it to function. If you wish to
- recompile the source you will need a copy of Borland's Delphi 2.0
- or later.
-
- Installing the TAxesView component:
- Included in with the source for the evaluator is a
- component that allows you to create a set of 2d axes. It is not
- very useful in and of itself, but it can be used as a base class
- from which to descend any object which needs to draw data on a
- grid in 2d space with axes. To install the component, use the
- normal Delphi 2 or 3 installation method outlined in the Delphi
- help files.
-
- Using the software
- --------------------------------------------------------------------
- The software is provided as a demo by which you can see the
- abilities of the expression evaluator. On the left side of the
- window is a button marked "Make Tree", as well as a data entry box
- and a second button marked "Calculate". The data entry box is used
- to enter the expression that you wish to evaluate. When the
- expression is fully and correctly typed in, click on the "Make Tree"
- button. This will generate an expression evaluation tree which will
- appear in the panel on the right of the window, as well as a list of
- parsing tokens which appear on the box under the "Tokens" tab. For
- more details on the evaluation tree, read further on in the
- documentation. To evaluate the expression once the tree has been
- built, just click on "Evaluate". This will evaluate your expression
- (assuming it is a valid expression), defaulting any unknown variables
- in the expression to 0.
- To change variable values, click on the "Variables" tab at
- the bottom of the window. A list of any variables that have already
- been created will appear. To create a new variable or change an
- existing one, simply type in the name and value in the data entry
- boxes provided and then click on "Set". The display will update
- accordingly. To delete a variable in the list, select it in the list
- and click on"Delete". To clear the list of variables, click on
- "Clear".
-
- The evaluation tree
- --------------------------------------------------------------------
- The evaluation tree at the right side of the window is a
- graphical display of the tree stored in memory used to evaluate the
- expressions. This is useful for debugging purposes and also helps
- to visualize how the evaluator works. The nice thing about using
- evaluation trees is that you only have to create them once for any
- given expression--including expressions that contain variables.
- The tree is evaluated from bottom to top by the use of a
- recursive routine. Operations with lower priority (lower in the
- order of operations) will tend to be higher on the tree, while
- operations of higher priority will tend to be lower. The exception
- to this is parentheses which override the standard order of
- operations and end up creating a smaller tree within the main tree.
- To read the tree, simply look at each symbol on the tree.
- Operations are represented by the standard entry symbol and each
- operation becomes a parental node on the tree. Each operation then
- has either more operations, numbers, or variables below it. Most
- operations are performed between the first and second value. For
- example:
- +
- |
- ----A
- |
- ----B
-
- This would mean A+B and this is the tree you would create if you
- type A+B into the data entry box and click on "Make Tree". The
- order really only becomes important when working with symbols such
- as '^'(exponent) or '/'(divide).
-
- Contacting the author
- --------------------------------------------------------------------
- I always welcome any comments and/or constructive criticisms
- regarding my work. If you have made any major changes that might
- prove useful, by all means share :-)
-
- Daniel Bryant--Email:
- COMPUSERVE: aabryant@compuserve.com
- MSN: allenbryant@msn.com
-
- I prefer that you use the compuserve account, but I also check my
- MSN mail occassionally.
-
- You can also post a message in the Compuserve Games Developers'
- Forum (GO GAMEDEV) to 75544,2762. I'm a frequent visitor so
- I'll be sure to get your message.
-
- Reference guide to the objects used for expression evaluation
- --------------------------------------------------------------------
- TTokenType = (ttUnknown,ttOperation,ttVariable,ttConstant);
-
- This is used to store the possible states for any given node in the
- tree. ttUnknown means that the value has not been defined yet.
- ttOperation means that this is some sort of operation that works on
- the values generated by its children nodes. ttVariable means that
- this is a variable that can change values over time. ttConstant
- means that this is a numeric constant that can be accessed directly.
-
- PVariableRecord = ^TVariableRecord;
- TVariableRecord = record
- VarData: Double;
- VarName: String;
- end;
-
- This record is used to store variable information. PVariableRecord
- is used to store pointers to variables in a TVarList.
-
- TVarList = class(TList)
- private
- function GetVars(Index: String): PVariableRecord;
- public
- property Vars[Index: String]: PVariableRecord read GetVars;
- //Methods
- destructor Destroy; override;
- function GetIndex(AValue: String): Integer;
- function NewVar(AName: String; AData: Double): PVariableRecord;
- end;
-
- This class based on TList is used as a method by which to store a
- list of variables. It has built-in support for accessing variables
- by name, as well as a convenient way to set/create variable data
- through NewVar--if the variable already exists it just changes
- whatever data there already is.
-
- TExpressionNode = class(TObject)
- private
- FValue: String;
- FParent: TExpressionNode;
- FTokenType: TTokenType;
- FChildren: TList;
- FValidValue: Boolean;
- FNumValue: Double;
- FVarPointer: PVariableRecord;
- FTree: TExpressionTree;
- public
- property Value: String read FValue write FValue;
- property Parent: TExpressionNode read FParent write FParent;
- property TokenType: TTokenType read FTokenType write FTokenType;
- property Children: TList read FChildren write FChildren;
- property ValidValue: Boolean read FValidValue write FValidValue;
- property NumValue: Double read FNumValue write FNumValue;
- property VarPointer: PVariableRecord read FVarPointer write FVarPointer;
- property Tree: TExpressionTree read FTree write FTree;
- //Methods
- function CreateChild: TExpressionNode;
- function Evaluate(SuppressErrors,UseVars: Boolean): Double;
- constructor Create(AOwner: TExpressionNode; ATree: TExpressionTree);
- destructor Destroy; override;
- end;
-
- This is an important part of the expression evaluator as it contains
- all of the data relevant to each node in the evaluation tree. It's
- value property simply contains the string token that spawned it--i.e.
- '+','100','D',etc. Parent is a pointer to its parent node. If Parent
- is nil then this node should be the top of the tree. TokenType
- contains information as to exactly what type of token it is--see the
- declaration for TTokenType enumerated type above. Children contains
- a list of all the children nodes this node has. ValidValue is used
- by the expression evaluator to determine if NumValue is currently
- valid--this can (and will be) used to write a procedure to simplify
- a given tree down to eliminate redundant constant operations with no
- variables. NumValue contains the numeric value at this point in the
- tree counting everything below it--it's only valid if ValidValue is
- true. VarPointer is used for variable nodes to keep a pointer to
- their associated variable. Tree contains a pointer back to the tree
- which contains the nodes. CreateChild is used to create a new child
- node. Evaluate is to evaluate the tree from this node onward. The
- constructor and destructor should be obvious.
-
- TExpressionTree = class(TPersistent)
- private
- FTopNode: TExpressionNode;
- FExpression: String;
- FCheckSyntax: Boolean;
- FTokens: TStringList;
- FVarList: TVarList;
- procedure SetExpression(AValue: String);
- public
- property TopNode: TExpressionNode read FTopNode write FTopNode;
- property Expression: String read FExpression write SetExpression;
- property CheckSyntax: Boolean read FCheckSyntax write FCheckSyntax;
- property Tokens: TStringList read FTokens write FTokens;
- property VarList: TVarList read FVarList write FVarList;
- //Methods
- constructor Create;
- destructor Destroy; override;
- procedure MakeTokens;
- procedure RemoveBadTokens;
- end;
-
- This class is used to contain the evaluation tree and also generate
- the tree. TopNode contains a pointer to the top node of the tree--
- this may or may not be nil at any given time. Expression contains
- the current expression that is in the evaluation tree--set this
- value to create a new tree. CheckSyntax, if set, will cause the
- object to check for syntax errors in the expression before the tree
- is created (this only checks for parentheses balance right now.)
- Tokens is a list of all of the tokens generated by the parser
- [MakeTokens]. VarList is a pointer to a variable list that can be
- used while evaluating the expression (not created automatically.)
- Call MakeTokens to create a rudimentary parse list from Expression
- and then call RemoveBadTokens to remove any redundant tokens that
- will cause undesired behavior (currently only checks for a single
- token enclosed in parentheses--i.e. '(A)')
-
- For reasons of length, I have decided not to include a reference to
- TAxesView or any of the other objects related to it, but with a
- little experimentation and a peek at the source it should not be too
- difficult to use and/or modify.
-
-
-
- LEGAL NOTICE:
- BOTH THIS SOFTWARE AND ALL ASSOCIATED MATERIALS INCLUDING SOURCE AND
- DOCUMENTATION ARE COPYRIGHTED FREEWARE. THEY MAY BE USED FREELY FOR
- ALL PURPOSES, COMMERCIAL AND NON-COMMERCIAL, AS LONG AS THE ORIGINAL
- AUTHOR OF THE SOFTWARE IS CREDITED FOR HIS WORK.